package recommendation;

import hibernate.method.GameMethods;
import hibernate.method.RelationMethods;
import hibernate.method.UserMethods;
import hibernate.model.GamesRate;
import hibernate.model.User;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Recommendation {
	
	
	public DataModel createGameDataModel(SessionFactory sessionFactory, Session session) throws IOException{		
		GameMethods gm = new GameMethods();
		List<GamesRate> list = new ArrayList<GamesRate>();
		list = gm.listAllGamesRates(sessionFactory, session);
		BufferedWriter bw = new BufferedWriter(new FileWriter("gamesTemp.csv"));
		
		for (GamesRate g: list){
			//System.out.println(g.getUser_id() + "," + g.getGame_id() + "," + g.getRate());
			bw.write(g.getUser_id() + "," + g.getGame_id() + "," + g.getRate() + ".0 \n");
		}
		bw.close();
		
		
		return new FileDataModel(new File("gamesTemp.csv"));
	}
	
	public DataModel createFriendDataModel(SessionFactory sessionFactory, Session session) throws IOException{		
		UserMethods um = new UserMethods();
		RelationMethods rm = new RelationMethods();
		List<User> list = new ArrayList<User>();
		list = um.list_by_type(sessionFactory, session, "client");
		BufferedWriter bw = new BufferedWriter(new FileWriter("friendsTemp.csv"));
		
		for (User u: list){
			System.out.println(u.getUser_id());
			List<User> friends = new ArrayList<User>();
			friends=rm.listAllFriends(sessionFactory, session, u.getUser_id());
			if (friends.size()!=0){
				for (User f : friends){
					System.out.println(u.getUser_id()+","+f.getUser_id()+ ","+ "1.0 \n");
					bw.write(u.getUser_id()+","+f.getUser_id()+ ","+ "1.0 \n");
				}	
			}
		}
		bw.close();
		
		
		return new FileDataModel(new File("friendsTemp.csv"));
	}
	
	
	
	
	public List<RecommendedItem> createGameRecommendationsForUser(SessionFactory sessionFactory, Session session, DataModel dm, int user_id, int items) throws TasteException{

		UserSimilarity similarity = new PearsonCorrelationSimilarity(dm);
		UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, dm);
		UserBasedRecommender recommender = new GenericUserBasedRecommender(dm, neighborhood, similarity);
		return recommender.recommend(user_id, items);

}
	
	
	public List<RecommendedItem> createFriendRecommendationsForUser(SessionFactory sessionFactory, Session session, DataModel dm, int user_id, int items) throws TasteException{

		UserSimilarity similarity = new PearsonCorrelationSimilarity(dm);
		UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.0, similarity, dm);
		UserBasedRecommender recommender = new GenericUserBasedRecommender(dm, neighborhood, similarity);
		return recommender.recommend(user_id, items);

}
}
